//
//  FRQChangePasswordViewController.m
//  FRQBluetoothKit_Example
//
//  Created by admin on 2023/10/24.
//  Copyright © 2023 xuchunhai. All rights reserved.
//

#import "FRQChangePasswordViewController.h"
#import "ChangePasswordViews.h"
#import "UILabel+YBAttributeTextTapAction.h"
#import "FRQResetPwdViewController.h"
#import "FRQAppDelegate.h"
@interface FRQChangePasswordViewController ()
@property (nonatomic,strong) ChangePasswordViews *ChangePasswordView;
@end

@implementation FRQChangePasswordViewController
- (ChangePasswordViews *)ChangePasswordView{
    if (nil==_ChangePasswordView) {
        _ChangePasswordView=skXibView(@"ChangePasswordViews");
        [self.view addSubview:_ChangePasswordView];
        [_ChangePasswordView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_topLayoutGuide);
            make.bottom.mas_equalTo(self.mas_bottomLayoutGuide);
            make.right.left.mas_equalTo(0);
        }];
        NSMutableParagraphStyle *style1 = [[NSMutableParagraphStyle alloc] init];
        style1.minimumLineHeight = 0;
        NSAttributedString *attributedString1 = [[NSAttributedString alloc] initWithString:@"Confirm new password" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style1,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _ChangePasswordView.txtNewpwd1.attributedPlaceholder = attributedString1;
        
        NSMutableParagraphStyle *style2 = [[NSMutableParagraphStyle alloc] init];
        style2.minimumLineHeight = 0;
        NSAttributedString *attributedString2 = [[NSAttributedString alloc] initWithString:@"New password" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style2,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _ChangePasswordView.txtNewpwd0.attributedPlaceholder = attributedString2;
        
        NSMutableParagraphStyle *style3 = [[NSMutableParagraphStyle alloc] init];
        style3.minimumLineHeight = 0;
        NSAttributedString *attributedString3 = [[NSAttributedString alloc] initWithString:@"Old password" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style3,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _ChangePasswordView.txtCpwd.attributedPlaceholder = attributedString3;
        @weakify(self)
        [[_ChangePasswordView.btnChange rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self changepassword];
        }];
        [_ChangePasswordView.viewContain mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.mas_equalTo(0);
            make.top.mas_equalTo(self.mas_topLayoutGuide).offset(75);
        }];
        
        _ChangePasswordView.labTip1.numberOfLines=0;
        
        [[_ChangePasswordView.btnForGetpassword rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            FRQResetPwdViewController *view=[[FRQResetPwdViewController alloc] init];
            view.modalPresentationStyle=0;
            view.viewType=0;
            NSDictionary *userDict=@{@"userName": skUser.accountNo};
            view.dicUser=userDict;
            [self.navigationController pushViewController:view animated:YES];
        }];
        //提醒2
        /*
         Passwords must be 8-16 characters and be some
         combination of letters, and/or special
         characters (cannot be only numbers)
         Forgot Password?
         */
        NSString *msgStr = _ChangePasswordView.labTip1.text;
        
        NSMutableAttributedString *attributedString0 = [[NSMutableAttributedString alloc]initWithString:msgStr];
        [attributedString0 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, msgStr.length)];
        [attributedString0 addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(msgStr.length-17, 17)];
        _ChangePasswordView.labTip1.attributedText = attributedString0;
        [_ChangePasswordView.labTip1 yb_addAttributeTapActionWithStrings:@[@"Forgot Password?"] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
            @strongify(self);
            FRQResetPwdViewController *view=[[FRQResetPwdViewController alloc] init];
            view.modalPresentationStyle=0;
            view.viewType=0;
            NSDictionary *userDict=@{@"userName": skUser.accountNo};
            view.dicUser=userDict;
            [self.navigationController pushViewController:view animated:YES];
//            [self presentViewController:view animated:YES completion:^{
//
//            }];
        }];
        
    }
    return _ChangePasswordView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.ChangePasswordView.hidden=NO;
    self.title=@"Password";
    // Do any additional setup after loading the view.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
/*
 /app/user/updatePw
 修改密码
 */
-(BOOL)checkPassWord:(NSString *)pwd
{
    //6-20位数字和字母组成
    NSString *regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
    NSPredicate *   pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if ([pred evaluateWithObject:pwd]) {
        return YES ;
    }else
        return NO;
}
-(void)changepassword{
    NSString *pwdC=[NSString stringWithFormat:@"%@",_ChangePasswordView.txtCpwd.text];
    
    NSString *pwdN0=[NSString stringWithFormat:@"%@",_ChangePasswordView.txtNewpwd0.text];
    NSString *pwdN1=[NSString stringWithFormat:@"%@",_ChangePasswordView.txtNewpwd1.text];
    
    pwdC=[PKNetTools md5:pwdC];
    pwdC=[PKNetTools md5:pwdC];

    if([self checkPassWord:pwdN1] ){
        if([pwdN0 isEqualToString:pwdN1]){
            pwdN1=[PKNetTools md5:pwdN1];
            pwdN1=[PKNetTools md5:pwdN1];
            
            
            NSDictionary *dic=@{@"password":pwdC,
                                @"newPassword":pwdN1
            };
            [skNetTools skPOSTWithURL:[NSString stringWithFormat:@"%@%@",skURL,@"app/user/updatePw"] parameters:dic cachePolicy:0 callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
                NSLog(@"数据=%@",responseObject);
                skNetModel *model=responseObject;
                [SkToast SkToastShow:model.msg];
                if(model.code==200){
                    _ChangePasswordView.labTip.hidden=NO;
                    _ChangePasswordView.labTip.textColor=skphUIColorFromRGB(0x44cd2c,0.8);
                    [_ChangePasswordView.viewContain mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.bottom.left.right.mas_equalTo(0);
                        make.top.mas_equalTo(self.mas_topLayoutGuide).offset(75+100);
                    }];
                    _ChangePasswordView.labTip.text=@"Your password has been updated \nsuccessfully.";
                }else{
                    _ChangePasswordView.labTip.hidden=NO;
                    _ChangePasswordView.labTip.textColor=skUIColorFromRGB(0xdd7023);
                    [_ChangePasswordView.viewContain mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.bottom.left.right.mas_equalTo(0);
                        make.top.mas_equalTo(self.mas_topLayoutGuide).offset(75+100);
                    }];
                    _ChangePasswordView.labTip.text=@"Please fill in the fields properly \nand submit again.";
                }
            }];
        }else{
            _ChangePasswordView.labTip.hidden=NO;
            _ChangePasswordView.labTip.textColor=skUIColorFromRGB(0xdd7023);
            [_ChangePasswordView.viewContain mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.left.right.mas_equalTo(0);
                make.top.mas_equalTo(self.mas_topLayoutGuide).offset(75+100);
            }];
            _ChangePasswordView.labTip.text=@"Please fill in the fields properly \nand submit again.";
        }
    }else{
////        Password cannot be only numbers
//        [SkToast SkToastShow:@"Password cannot be only numbers"];
        _ChangePasswordView.labTip.hidden=NO;
        _ChangePasswordView.labTip.textColor=skUIColorFromRGB(0xdd7023);
        [_ChangePasswordView.viewContain mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.mas_equalTo(0);
            make.top.mas_equalTo(self.mas_topLayoutGuide).offset(75+100);
        }];
        _ChangePasswordView.labTip.text=@"Please fill in the fields properly \nand submit again.";
    }
    
}
@end
